// pages/profile/index.js
const userApi = require('../../api/user');
const app = getApp();

Page({
  data: {
    userInfo: null,
    levelName: '新手',
    progress: 0,
    myEventsCount: 0,
    myClubsCount: 0
  },

  onLoad() {
    this.loadUserData();
  },

  onShow() {
    // 每次显示页面时刷新数据
    if (app.globalData.isLogin) {
      this.loadUserData();
    } else {
      this.setData({ userInfo: null });
    }
  },

  /**
   * 加载用户数据
   */
  loadUserData() {
    if (!app.globalData.isLogin) {
      return;
    }

    userApi.getUserInfo()
      .then(data => {
        // 计算等级信息
        const levelInfo = this.calculateLevelInfo(data.points || 0);

        this.setData({
          userInfo: data,
          levelName: levelInfo.name,
          progress: levelInfo.progress
        });

        // 加载统计数据
        this.loadStatistics();
      })
      .catch(err => {
        console.error('加载用户信息失败:', err);
      });
  },

  /**
   * 加载统计数据
   */
  loadStatistics() {
    // TODO: 调用统计API
    // 这里使用模拟数据
    this.setData({
      myEventsCount: 3,
      myClubsCount: 2
    });
  },

  /**
   * 计算等级信息
   */
  calculateLevelInfo(points) {
    const levels = [
      { level: 1, name: '新手', min: 0, max: 99 },
      { level: 2, name: '进阶', min: 100, max: 299 },
      { level: 3, name: '熟练', min: 300, max: 599 },
      { level: 4, name: '专业', min: 600, max: 999 },
      { level: 5, name: '大师', min: 1000, max: 1999 },
      { level: 6, name: '传奇', min: 2000, max: Infinity }
    ];

    for (let i = 0; i < levels.length; i++) {
      const level = levels[i];
      if (points >= level.min && points <= level.max) {
        const progress = level.max === Infinity 
          ? 100 
          : Math.round(((points - level.min) / (level.max - level.min + 1)) * 100);
        
        return {
          ...level,
          progress
        };
      }
    }

    return levels[0];
  },

  /**
   * 处理登录
   */
  handleLogin() {
    app.login((success) => {
      if (success) {
        this.loadUserData();
      }
    });
  },

  /**
   * 处理退出登录
   */
  handleLogout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          app.logout();
          this.setData({ userInfo: null });
        }
      }
    });
  },

  /**
   * 导航到设置页
   */
  navigateToSettings() {
    wx.navigateTo({
      url: '/pages/profile/settings'
    });
  },

  /**
   * 导航到积分页
   */
  navigateToPoints() {
    wx.navigateTo({
      url: '/pages/profile/points'
    });
  },

  /**
   * 导航到指定页面
   */
  navigateTo(e) {
    const { url } = e.currentTarget.dataset;
    
    if (!app.globalData.isLogin) {
      this.handleLogin();
      return;
    }

    wx.navigateTo({
      url
    });
  },

  /**
   * 显示关于我们
   */
  showAbout() {
    wx.showModal({
      title: 'Fun-Sport',
      content: '版本 1.0.0\n\n一个帮助运动爱好者找到志同道合伙伴的社交平台',
      showCancel: false
    });
  },

  /**
   * 分享
   */
  onShareAppMessage() {
    return {
      title: '一起来运动吧！',
      path: '/pages/index/index'
    };
  }
});

